package page.sample.userpackage;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import page.sample.AuthorizationPage;
import page.sample.administratorpackage.ShowGoodsPage;
import dao.impl.GoodsDao;
import dao.impl.UserDao;
import entity.Goods;
import entity.User;

public class ShowShoppingCartPage extends WebPage {

	public ShowShoppingCartPage(final User user)
	{
		final GoodsDao goodsDao = new GoodsDao(
				app.MyFirstWebApplication.DATA_XML_FOLDER);
		final UserDao userDao = new UserDao(
				app.MyFirstWebApplication.DATA_XML_FOLDER);

		add(new DataView<Goods>("table-rows", new ListDataProvider<Goods>(
				user.getShoppingCart())) {
			@Override
			protected void populateItem(final Item<Goods> item) {
				final Goods goods = item.getModelObject();
				// add data for table cell in row
				item.add(new Label("name", goods.getName()));
				item.add(new Label("price", goods.getPrice()));
				
				item.add(new Link("deleteGoods") {
					@Override
					public void onClick() {
						
						user.getShoppingCart().remove(goods);
						userDao.update(user);
						
						setResponsePage(new ShowShoppingCartPage(user));
					}
					});
			}
		});
		
		add(new Link("buy") {

			@Override
			public void onClick() {
				
				setResponsePage(new BuyPage(user));
				
			}

		});

		add(new Link("showShoppingCart") {

			@Override
			public void onClick() {
				setResponsePage(new ShowShoppingCartPage(user));
			}

		});
		
		add(new Link("exit") {

			@Override
			public void onClick() {
				setResponsePage(new AuthorizationPage());

			}

		});
	}
}
